from fastapi import APIRouter, Depends
from sqlmodel import Session
from crud import dic as dic_crud
from common import response_code
import models
from dependencies import engine, SessionDep
from typing import Annotated, List
from routers.auth import get_current_sys_user

router = APIRouter()

"""查询字典列表数据

Keyword arguments:
offset -- 起始页 (default 0)
limit -- 每页条数 (default 20)
name -- 字典名称 (default None)
code -- 字典编码 (default None)
type -- 字典类型 (default None)
type_code -- 字典类型编码 (default None)
Return: 字典列表数据
"""


@router.get(
    "/api/system/dict/",
    tags=["字典管理"],
    summary="查询字典",
    response_model=models.RespJsonDic,
)
async def get_dict(
    skip: int = 0,
    limit: int = 20,
    name: str | None = None,
    code: int | None = None,
    type: str | None = None,
    type_code: str | None = None,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)] = None,
    session: SessionDep = Session(engine),
    response: models.RespJsonDic = Depends(response_code.RespSuccessJson.resp_200),
):
    content = dic_crud.get_dict(skip, limit, name, code, type, type_code, user, session)

    response.data = {
        "total": content[1],
        "totalPages": content[1] // limit + 1 if content[1] % limit else content[1] // limit,
        "items": [dict(item) for item in content[0]] if content[0] else [],
    }
    response.msg = "查询成功"

    return response


"""新增字典

Keyword arguments:
name -- 字典名称
code -- 字典编码
type -- 字典类型
type_code -- 字典类型编码
Return: response -- 成功提示
"""


@router.post(
    "/api/system/dict/",
    tags=["字典管理"],
    summary="新增字典",
    response_model=models.RespJsonDic,
)
async def create_dict(
    dic: models.SystemDictCreate,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)],
    session: SessionDep = Session(engine),
    response: models.RespJsonDic = Depends(response_code.RespSuccessJson.resp_201),
):
    # 新增字段
    dic_crud.create_dict(dic, user, session)

    response.msg = "新增成功"
    return response


"""编辑字典

Keyword arguments:
id -- 字典id
name -- 字典名称
code -- 字典编码
type -- 字典类型
type_code -- 字典类型编码
Return: response -- 成功提示
"""


@router.put(
    "/api/system/dict/{id}",
    tags=["字典管理"],
    summary="编辑字典",
    response_model=models.RespJsonDic,
)
async def update_dict(
    id: int,
    dict: models.SystemDictUpdate,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)] = None,
    session: SessionDep = Session(engine),
    response: models.RespJsonDic = Depends(response_code.RespSuccessJson.resp_202),
):
    # 更新字段
    dic_crud.update_dict(id, dict, user, session)

    response.msg = "编辑成功"
    return response


"""删除字典

Keyword arguments:
id -- 字典id
Return: response -- 成功提示
"""


@router.delete(
    "/api/system/dict/",
    tags=["字典管理"],
    summary="删除字典",
    response_model=models.RespJsonDic,
)
async def delete_dict(
    ids: List[int] ,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)] = None,
    session: SessionDep = Session(engine),
    response: models.RespJsonDic = Depends(response_code.RespSuccessJson.resp_204),
):
    # 删除字段
    for id in ids:
        dic_crud.delete_dict(id, user, session)
    
    response.msg = "删除成功"
    return response
